package io.seata.sample.controller;

import io.seata.sample.service.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class BusinessController {
	
	@Autowired
	private BusinessService businessService;
	
	/**
	 * 购买下单，模拟全局事务提交
	 *
	 * @return
	 */
	@RequestMapping("/purchase/commit")
	public Boolean purchaseCommit() {
		new Thread(() -> {
			businessService.purchase("U100000", "W10000", 1);
		}).start();
		new Thread(() -> {
			businessService.purchase("U100001", "W10000", 1);
		}).start();
		new Thread(() -> {
			businessService.purchase("U100001", "W10000", 1);
		}).start();
		new Thread(() -> {
			businessService.purchase("U100001", "W10000", 1);
		}).start();
		
		return true;
	}
	
	/**
	 * 购买下单，模拟全局事务提交
	 *
	 * @return
	 */
	@RequestMapping("/purchase/commit1")
	public Boolean purchaseCommit1() {
		businessService.purchase("U100000", "W10000", 1);
		return true;
	}
	
	/**
	 * 购买下单，模拟全局事务回滚
	 *
	 * @return
	 */
	@RequestMapping("/purchase/rollback")
	public Boolean purchaseRollback() {
		businessService.purchase("U100000", "W10000", 1);
		return true;
	}
}
